from __future__ import print_function
import ctypes, sys
import os
import time

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False
if is_admin():
    fo = open("log.txt", "a+",encoding='utf-8')  #a+  意思是向后追加 w是直接写入  前面的要覆盖 r是只有只读
    localtime =  (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    str="启动时间 :"+localtime+"\n"
    fo.write(str)
    fo.flush()
    with os.popen(r'net stop mysql5.7', 'r') as f:
        text = f.read()
        fo.write(text)
        fo.flush()
        #.\mysqladmin.exe -uroot -proot shutdown 关闭mysql
        #.\mysqld.exe --console  启动MySQL  
        #.\redis-server.exe .\redis.windows.conf
        #D:\soft\redis\Redis-x64-3.2.100 (1)\redis.windows.conf
    with os.popen(r'D:\soft\mysql8\mysql-8.0.19-winx64\bin\mysqld.exe --console', 'r') as f:
        fo.write("启动mysql8\n")
        fo.flush()
    
    #    with os.popen(r'C:\Users\Administrator\Desktop\test.bat', 'r') as f:
    #    fo.write("启动redis\n")
    
    # 刷新缓冲区
    fo.flush()
    # 关闭文件
    fo.close()
else:
    if sys.version_info[0] == 3:
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
    else:#in python2.x
        ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(sys.executable), unicode(__file__), None, 1)
















